﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using StockMatch.Service.Entity;
using ThreadTask;
using Titan;

namespace StockMatch.Host.Task
{
    public class DayWinProjectManager : TaskBase
    {
        public int ProjectId { get; set; }

        public override void DoWork()
        {
            DayWinProject dayWinProject = new DayWinProject {ProjectId = ProjectId};

            #region 查询当天是否已经支付管理费
            IDbSession cn1 = Environment.OpenSession();
            IDataReader dateReader = null;
            try
            {
                dateReader = cn1.ExecuteReader("select * from daywinProjectaccount where date(paytime) = curdate();");
                if (dateReader != null && dateReader.Read())
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                if (dateReader != null && !dateReader.IsClosed)
                {
                    dateReader.Close();
                }
                cn1.Close();
                throw new Exception(ex.Message);
            }
            finally
            {
                if (dateReader != null && !dateReader.IsClosed)
                {
                    dateReader.Close();
                }
                cn1.Close();
            }
            #endregion

            using (IDbSession cn = Environment.OpenSession())
            {
                if (!cn.Select(dayWinProject,DayWinProject_.ALL,DayWinProject_.User.ALL))
                {
                    return;
                }
            }

            using (IDbSession cn = Environment.OpenSession())
            {
                try
                {
                    cn.BeginTransaction();
                    User user = dayWinProject.User;
                    user.ManagAmount += dayWinProject.ManageMoney; //管理费
                    bool a = cn.Update(user,User_.ManagAmount) > 0;

                    //支付账号管理费
                    DayWinProjectAccount projectAccount = new DayWinProjectAccount();
                    projectAccount.ProjectId = dayWinProject.ProjectId;
                    projectAccount.PayMoney = dayWinProject.ManageMoney;
                    projectAccount.PayState = 1;
                    projectAccount.PayTime = DateTime.Now;
                    bool b = cn.Insert(projectAccount) > 0;

                    if (a && b)
                    {
                        cn.Commit();
                    }
                    else
                    {
                        cn.Rollback();
                    }
                }
                catch (Exception ex)
                {
                    cn.Rollback();
                    throw new Exception(ex.Message);
                }
            }
            Console.WriteLine("正在执行Task.....");
            Thread.Sleep(1000*2);
        }

        public override void OnWorkBegin()
        {
            base.OnWorkBegin();
            Console.WriteLine("开始执行Taks：DayWinProjectManager");
        }

        public override void OnWorkError(Exception exception)
        {
            base.OnWorkError(exception);
            Console.WriteLine("执行Taks：DayWinProjectManager出错");
        }

        public override void OnWorkCompleted()
        {
            base.OnWorkCompleted();
            Console.WriteLine("执行Taks：DayWinProjectManager完成");

        }
    }
}
